package com.gxa.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.vo.DrugNumRecordVo;
import com.gxa.common.vo.DrugVo;
import com.gxa.controller.lgr.fileUpLoad.UploadAPI;
import com.gxa.entity.SysDrug;
import com.gxa.entity.SysProvider;
import com.gxa.mapper.DrugManagementMapper;
import com.gxa.service.DrugManagementService;
import com.gxa.service.SysDrugTotalService;
import com.gxa.utils.FileUtils;
import com.gxa.utils.HeaderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.gxa.utils.FileUtils.DRUG_AVATAR;

/**
 * @author 93024
 */
@Service
public class DrugManagementServiceImpl implements DrugManagementService {

    @Autowired
    DrugManagementMapper mapper;

    @Autowired
    SysDrugTotalService sysDrugTotalService;

    @Override
    public R findDrugList(Integer currentPage, Integer pageSize) {
        R r = new R();

        PageHelper.startPage(currentPage,pageSize);
        List<DrugVo> list = mapper.findDrugList();
        PageInfo<DrugVo> pageInfo = new PageInfo(list);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(pageInfo);
        return r;
    }

    @Override
    public R chooseProvider(Integer providerId) {
        R r = new R();

        SysProvider res = mapper.findProviderById(providerId);
        if (res != null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(res);
            return r;
        }else {
            r.setCode(400);
            r.setMsg("没有这个供应商");
            return r;
        }

    }

    @Override
    public R addDrug(SysDrug sysDrug) {
        R r = new R();
        int res =  mapper.addDrug(sysDrug);
        Integer id = sysDrug.getId();
        if (res>0 && id != null){
           r.setCode(200);
           r.setMsg("添加成功");
           r.setData(id);
           return r;
       }else {
           r.setCode(400);
           r.setMsg("添加失败");
           return r;
       }

    }
    @Override
    public R deleteDrug(Integer id) {
        R r = new R();
        int res = mapper.updateDrugIsDeleteById(id);
        if (res>0){
            r.setCode(200);
            r.setMsg("删除成功");
            return r;
        }else {
            r.setCode(400);
            r.setMsg("删除失败，也许没有这条数据");
            return r;
        }

    }

    @Override
    public R findDrugById(Integer id) {
        R r = new R();
        DrugVo res = mapper.findDrugById(id);
        if (res != null){
            r.setCode(200);
            r.setMsg("查找成功");
            r.setData(res);
            return r;
        }else {
            r.setCode(400);
            r.setMsg("查找成功，也许没有这条数据");
            return r;
        }

    }

    @Override
    public R updateDrug(SysDrug sysDrug) {
        R r = new R();
        int res =  mapper.updateDrug(sysDrug);
        if (res>0){
            r.setCode(200);
            r.setMsg("修改成功");
            return r;
        }else {
            r.setCode(400);
            r.setMsg("修改失败");
            return r;
        }
    }

    @Override
    public R updateDrugNum(DrugNumRecordVo numRecordVo, HttpServletRequest request) {
        R r = new R();
        Integer type = numRecordVo.getType();
        Integer drugId = numRecordVo.getId();
        Integer num = numRecordVo.getNum();
        String remark = numRecordVo.getRemark();
        Integer userId = HeaderUtils.getUserID(request);
        if (type == 1){
            sysDrugTotalService.drugRecord(type,drugId,num,remark,userId);
            r.setCode(200);
            r.setMsg("入库成功"+num);
            return r;
        }
        if (type == 0){
            sysDrugTotalService.drugRecord(type,drugId,num,remark,userId);
            r.setCode(200);
            r.setMsg("出库成功"+num);
            return r;
        }

        r.setCode(400);
        r.setMsg("错误");
        return r;
    }

    @Override
    public R findDrugByCondition(Integer currentPage, Integer pageSize, String condition) {

        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List<DrugVo> list = mapper.findDrugByCondition(condition);
        PageInfo<DrugVo> pageInfo = new PageInfo(list);
        if (pageInfo !=null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(pageInfo);
            return r;
        }else {
            r.setCode(400);
            r.setMsg("查询失败");
            return r;
        }
    }

    @Override
    public R addDrugImg(MultipartFile img, Integer id,HttpServletRequest request) {
        R r = new R<>();
        try{
            String imgUrl = FileUtils.saveImage(img, request, 0);
            int res = mapper.addDrugImg(imgUrl,id);
            r.setCode(200);
            r.setMsg("添加图片成功");
            return r;
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
            return r;
        }
    }

}
